import  time
import json
import sys
import jieba.posseg as pseg
import jieba
import pandas as pd
import numpy as np
from util.model_log import create_log
from sklearn import preprocessing
import time

#解析单条
def data_handle_inference(json_data, word_label_enc, word_set):

    dialog = json_data["dialog"]
    words_list = []

    for one_dialog in dialog:
        t = time.time()
        seg_list = pseg.cut(one_dialog["text"])
        # self.logger.info("cut word cost time is:" + str(time.time() - t))
        for word, characteristic in seg_list:
            t1 = time.time()
            # 过滤标点符号等东西
            if characteristic != 'x' \
                    and characteristic != 'y' \
                    and characteristic != 'z' \
                    and characteristic != 'l' \
                    and characteristic != 'm' \
                    and word in word_set:
                #如果没有则继续

                try:
                    words_list.append(word)
                except:
                    continue

    words_list = word_label_enc.transform(words_list)
    words_list = list(words_list)
    embedding_size = 1348469
    #padding
    if len(words_list) > 300:
        words_list.append(words_list[:300])

    else:
        words_list = words_list + [embedding_size -1] * (300 - len(words_list))

    words_list = np.array([words_list])
    return words_list

def model_inference(model,x_inference,sub_task,detail_mask,threshold):


    intent_output, subtask_output, subtask_detail_output = model.predict(x_inference)
    #mask 操作
    sub_task = np.dot(intent_output,sub_task)
    sub_task[sub_task > threshold] = 1
    sub_task[sub_task <= threshold] = 0

    subtask_output = np.multiply(subtask_output,sub_task)
    detail_mask = np.dot(subtask_output,detail_mask)
    detail_mask[detail_mask > threshold] = 1
    detail_mask[detail_mask <= threshold] = 0
    subtask_detail_output = np.multiply(subtask_detail_output,detail_mask)

    #进行0 1变化
    #intent 设置为高阈值
    intent_output[intent_output > 0.5] = 1
    intent_output[intent_output <= 0.5] = 0
    subtask_output[subtask_output > threshold] = 1
    subtask_output[subtask_output <= threshold] = 0
    subtask_detail_output[subtask_detail_output > threshold] = 1
    subtask_detail_output[subtask_detail_output <= threshold] = 0


    return intent_output, subtask_output, subtask_detail_output